package com.mxc.gift.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.lww.springcloudapi.entity.TabEmployee;
import com.lww.springcloudapi.entity.TabGgcardObtain;
import com.lww.springcloudapi.entity.TabSaleplanGiftcode;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import com.mxc.gift.service.TabSaleplanGiftcodeService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.ApiOperation;
import org.eclipse.jetty.util.ajax.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 销售礼品编码定义表 前端控制器
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/giftcode")
public class TabSaleplanGiftcodeController {

    @Autowired
    TabSaleplanGiftcodeService tabSaleplanGiftcodeService;

    /**
     * 礼品录入
     *
     * @param tabSaleplanGiftcode
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("礼品录入")
//    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Result insertGiftCode( TabSaleplanGiftcode tabSaleplanGiftcode,TabEmployee tabEmployee) {
        try {
            tabEmployee.setAcptsiteid("46").setEname("小乐");
            boolean b = tabSaleplanGiftcodeService.insertGiftCode(tabSaleplanGiftcode,tabEmployee);
            return Result.ok(b);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    /**
     * 礼品修改
     *
     * @param tabSaleplanGiftcode
     * @return
     */
    @PostMapping("/upd")
    @ApiOperation("礼品修改")
//    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Result addGiftCode(TabSaleplanGiftcode tabSaleplanGiftcode) {
        try {
            TabEmployee tabEmployee = new TabEmployee();
            System.out.println(tabSaleplanGiftcode);
            tabEmployee.setEname("小乐").setAcptsiteid("46");
            boolean b = tabSaleplanGiftcodeService.updGiftCode(tabSaleplanGiftcode, tabEmployee);
            return Result.ok(b);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    /**
     * 礼品删除
     *
     * @param
     * @return
     */
    @PostMapping("/del/{giftcide}")
    @ApiOperation("礼品删除")
//    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Result delGiftCode(@PathVariable("giftcide") String giftcide) {
        try {
            System.out.println(giftcide);
            boolean b = tabSaleplanGiftcodeService.delGiftCode(giftcide);
            return Result.ok(b);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    /**
     * 查询
     *
     * @param pageNo
     * @param pageSize
     * @param giftname,礼品名称
     * @param prizelevel，礼品等级
     * @param
     * @return
     */
    @PostMapping("/pagecode")
    @ApiOperation("分页连表查询")
//    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Result queryByGifctode(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                  @RequestParam(required = false) String giftname,
                                  @RequestParam(required = false) String prizelevel,
                                  TabEmployee tabEmployee) {
        try {
            tabEmployee.setAcptsiteid("46");
            List<TabSaleplanGiftcode> tabSaleplanGiftcodes = tabSaleplanGiftcodeService.queryByGiftcode(pageNo, pageSize, giftname, prizelevel, tabEmployee.getAcptsiteid());
              PageInfo<TabSaleplanGiftcode> pageInfo = new PageInfo<TabSaleplanGiftcode>(tabSaleplanGiftcodes);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }


    //服务熔断
    public String hystrixGet() {
        return "出现异常！！";
    }
}

